package com.scenic.rd_android.testview.lstview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

import com.nova.scenic.projectlibs.util.debug.MyLog;
import com.scenic.rd_android.R;

import java.net.HttpURLConnection;
import java.net.URL;

/**
     * 原文地址: http://blog.csdn.net/guolin_blog/article/details/45586553
     * 使用 findViewWithTag 来解决图片错位
     * @author guolin
     */
    public class ImageAdapter extends ArrayAdapter<String> {


    private static final String TAG = "ImageAdapter";
    /**
         * 图片缓存技术的核心类，用于缓存所有下载好的图片，在程序内存达到设定值时会将最少最近使用的图片移除掉。
         */
        private LruCache<String, BitmapDrawable> mMemoryCache;
    private View mListView;

    public ImageAdapter(Context context, int resource, String[] objects) {
            super(context, resource, objects);
            // 获取应用程序最大可用内存
            int maxMemory = (int) Runtime.getRuntime().maxMemory();
            int cacheSize = maxMemory / 8;
            MyLog.i(TAG, "max memory is " + maxMemory);
            mMemoryCache = new LruCache<String, BitmapDrawable>(cacheSize) {
                @Override
                protected int sizeOf(String key, BitmapDrawable drawable) {
                    return drawable.getBitmap().getByteCount();
                }
            };
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(mListView == null){
                mListView = parent;
            }
            String url = getItem(position);
            View view;
            if (convertView == null) {
                view = LayoutInflater.from(getContext()).inflate(R.layout.adapter_listview_load_image_ectopic, null);
            } else {
                view = convertView;
            }
            ImageView image = (ImageView) view.findViewById(R.id.image);

            /////////
            image.setTag(url);
            /////////

            BitmapDrawable drawable = getBitmapFromMemoryCache(url);
            if (drawable != null) {
                image.setImageDrawable(drawable);
            } else {
                BitmapWorkerTask task = new BitmapWorkerTask(image);
                task.execute(url);
            }
            return view;
        }

        /**
         * 将一张图片存储到LruCache中。
         *
         * @param key
         *            LruCache的键，这里传入图片的URL地址。
         * @param drawable
         *            LruCache的值，这里传入从网络上下载的BitmapDrawable对象。
         */
        public void addBitmapToMemoryCache(String key, BitmapDrawable drawable) {
            if (getBitmapFromMemoryCache(key) == null) {
                mMemoryCache.put(key, drawable);
            }
        }

        /**
         * 从LruCache中获取一张图片，如果不存在就返回null。
         *
         * @param key
         *            LruCache的键，这里传入图片的URL地址。
         * @return 对应传入键的BitmapDrawable对象，或者null。
         */
        public BitmapDrawable getBitmapFromMemoryCache(String key) {
            return mMemoryCache.get(key);
        }

        /**
         * 异步下载图片的任务。
         *
         * @author guolin
         */
        class BitmapWorkerTask extends AsyncTask<String, Void, BitmapDrawable> {

            private ImageView mImageView;
            private String imageUrl;

            public BitmapWorkerTask(ImageView imageView) {
                mImageView = imageView;
            }

            @Override
            protected BitmapDrawable doInBackground(String... params) {
                imageUrl = params[0];
                // 在后台开始下载图片
                Bitmap bitmap = downloadBitmap(imageUrl);
                BitmapDrawable drawable = new BitmapDrawable(getContext().getResources(), bitmap);
                addBitmapToMemoryCache(imageUrl, drawable);
                return drawable;
            }

            @Override
            protected void onPostExecute(BitmapDrawable drawable) {

                /////////
                mImageView = (ImageView) mListView.findViewWithTag(imageUrl);
                /////////

                if (mImageView != null && drawable != null) {
                    mImageView.setImageDrawable(drawable);
                }
            }

            /**
             * 建立HTTP请求，并获取Bitmap对象。
             *
             * @param imageUrl
             *            图片的URL地址
             * @return 解析后的Bitmap对象
             */
            private Bitmap downloadBitmap(String imageUrl) {
                Bitmap bitmap = null;
                HttpURLConnection con = null;
                try {
                    URL url = new URL(imageUrl);
                    con = (HttpURLConnection) url.openConnection();
                    con.setConnectTimeout(5 * 1000);
                    con.setReadTimeout(10 * 1000);
                    bitmap = BitmapFactory.decodeStream(con.getInputStream());
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (con != null) {
                        con.disconnect();
                    }
                }
                return bitmap;
            }

        }

    }
